﻿using GenericHostDemo.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericHostDemo
{
    /// <summary>
    /// 服务集合容器
    /// </summary>
    public class ServiceCollection: IServiceCollection
    {
        private readonly Dictionary<Type, object> _services = new();
        public void AddSingleton<TService>(TService implementation)
        {
            _services[typeof(TService)] = implementation;
        }
        public IServiceProvider BuildServiceProvider()
        {
            return new ServiceProvider(_services);
        }

        public Dictionary<Type, object> GetServices()
        {
            return _services;
        }
    }

    public class ServiceProvider : IServiceProvider
    {
        private readonly Dictionary<Type, object> _services;
        public ServiceProvider(Dictionary<Type, object> services)
        {
            _services = services;
        }
        public object GetService(Type serviceType)
        {
            _services.TryGetValue(serviceType, out var service);
            return service;
        }
    }
}
